/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager;

import org.zhiqim.kernel.logging.Log;
import org.zhiqim.kernel.logging.LogFactory;
import org.zhiqim.kernel.schedule.Task;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Sqls;
import org.zhiqim.manager.dao.ZmrParamDao;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.dbo.ZmrOperatorLog;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 管理台任务
 * 1）检查超级管理员是否需要关闭
 * 2）删除保留时长之外的操作日志
 *
 * @version v1.0.0 @author zouzhigang 2017-7-19 新建与整理
 */
public class ZmrTask implements Task
{
    private static final Log log = LogFactory.getLog(ZmrTask.class);
    
    @Override
    public void execute()
    {
        //检查超级管理员是否需要关闭
        chkCloseSuperAdmin();
        
        //删除保留时长之外的操作日志
        delExpireOperateLog();
    }
    
    /** 检查超级管理员是否需要关闭 */
    private void chkCloseSuperAdmin()
    {
        try
        {
            if (!ZmrParamDao.chkCloseSuperAdmin())
            {//无需检查是否需要关闭超级管理员
                return;
            }
            
            if (ORM.table().count(ZmrOperator.class, new Selector("operatorType", 0).addMust("operatorValid", true)) == 0)
            {//超级管理员已停用的不处理
                return;
            }
            
            if (ORM.table().count(ZmrOperator.class, new Selector("operatorType", 1).addMust("operatorValid", true)) == 0)
            {//如果没有正常的管理员，则不停用超级管理员
                return;
            }
            
            //有正常的管理员，则停用超级管理员
            ORM.table().update(ZmrOperator.class, new Updater().addField("operatorValid", false).addMust("operatorType", 0));
            log.info("停用超级管理员成功...");
        }
        catch (Exception e)
        {
            log.error("停用超级管理员时异常", e);
        }
    }
    
    /** 删除保留时长之外的操作日志 */
    private void delExpireOperateLog()
    {
        try
        {
            int remainDay = ZmrParamDao.getRemainLogDay();
            String datetime = DateTimes.getDateString() + " 00:00:00";
            String operateTime = DateTimes.getPreviousDateTimeString(datetime, remainDay);
            
            Selector selector = new Selector()
                .addMustThenL("operateTime", Sqls.toTimestamp(operateTime));
            
            ORM.table().delete(ZmrOperatorLog.class, selector);
            log.info("删除[%s]之前的操作日志成功...", operateTime);
        }
        catch (Exception e)
        {
            log.error("删除之前的操作日志时异常", e);
        }
    }
}
